# C:/Python27/TextResuming/node_tree.py
# This is node_tree file
# Author : @ismailsunni

import util as util

class node_tree:
	'''a class defining a node_tree.
		left, right is a node_tree
		depth is depth measure from base node_tree.'''

	def __init__(self, left = None, right = None, depth = 0, content = []):
		self.content = content
		self.left = left
		self.right = right
		self.depth = depth

	def merge_node(self, other_node_tree):
		'''return a node_tree from self and other_node_tree.
			content will be merge, depth will be increased by 1.'''

		left = self
		right = other_node_tree
		depth = max(left.depth, right.depth) + 1
		if isinstance(self.content, list):
			content = self.content + list(other_node_tree.content)	
		else:
			content = set(self.content) | set(other_node_tree.content) 

		retval = node_tree(left, right, depth, content)

		return retval

	def is_leaf(self):
		'''Return true if left or right == None. It is leaf.'''

		return self.left == None or self.right == None

	def print_node_tree(self):
		'''print function.'''

		print "content : ", self.content 
		print "depth : ", self.depth

	def get_size(self):
		'''Get size of content of a node'''

		return len(self.content)

	def visualize(self, i):
		'''visualize the tree. i is initial tabulation'''

		if self.is_leaf():
			print '---'*(i) + str(self.content)
		else:
			print '---'*(i) + str(self.content)
			self.left.visualize(i+1)
			self.right.visualize(i+1)

	def get_term(self, list_term):
		'''Return list of term in a node_tree.'''

		retval = []
		for idx in self.content:
			if idx < len(list_term):
				retval.extend(list_term[idx])

		return retval

def get_list_content(list_node_tree):
	'''return list of content from list of node_tree.'''

	list_content = []
	for node_tree in list_node_tree:
		list_content.append(node_tree.content)

	return list_content

def main():
	import __main__
	util.debug(__main__.__file__)
	util.debug('main')	

if __name__ == '__main__':
	main()